package com.luiz.aero.android.computation;


import android.content.Context;

import com.luiz.aero.android.data.Position;
import com.luiz.aero.android.data.Route;

public class RouteComputation extends Thread{

	private Route route;
	private Context context;
	private ChartGenerator chartGenerator;

	public RouteComputation(Context context) {
		super();
		Position inicialPosition = new Position();
		this.route = new Route(inicialPosition);
		this.context = context;
		chartGenerator = new ChartGenerator();
	}

	public void run(){
		Position pos;
		while(planeIsFlying()){
			try {
				pos = new Position();
				if(isNewPositionReallyFar(pos)){
					route.addPosition(pos);
				}
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		chartGenerator.generateAllCharts(route);
		chartGenerator.saveObjects();
	}

	private boolean planeIsFlying() {
		if(System.getProperty("is-flying").equals("true")){
			return true;
		}else{
			return false;
		}
	}

	private boolean isNewPositionReallyFar(Position pos) {
		Position lastPosition = route.getLastPosition();
		float distanceFromLastToNew = pos.getLocation().distanceTo(lastPosition.getLocation());
		if(distanceFromLastToNew > 3){
			return true;
		}
		return false;
	}

	public Route getRoute() {
		return route;
	}



}
